package tianxing

import (
	"encoding/json"
	"fmt"
	ms "gin_template/core/request"
	"strconv"

	"gin_template/data/weather"

	"go.uber.org/zap"
)

type WeatherConf struct {
	Config
	City string `json:"city"`
}

type WeatherResult struct {
	Common
	Newslist []WeatherValue `json:"newslist"`
}

type WeatherValue struct {
	Area    string `json:"area"`
	Date    string `json:"date"`
	Week    string `json:"week"`
	Weather string `json:"weather"`
	Real    string `json:"real"`
	Lowest  string `json:"lowest"`
	Highest string `json:"highest"`
	Wind    string `json:"wind"`
	Sunrise string `json:"sunrise"`
	Sunset  string `json:"sunset"`
	Tips    string `json:"tips"`
}

func RequestSayWeather(config interface{}) string {
	conf := config.(*WeatherConf)
	result := &WeatherResult{}
	res := ms.RequestGet(conf.Url + "/tianqi/index?key=" + conf.Key + "&city=" + conf.City)
	err := json.Unmarshal(res, &result)
	if err != nil {
		zap.L().Info("RequestSayWeather序列化失败", zap.Error(err))
	}
	fmt.Println(result)
	if result.Code == 150 {
		return "今天天气机器人卡壳啦，请充值让我修复它吧，hhh"
	}
	var todayWeather = result.Newslist[0]

	info := "，你好：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;今天是" + todayWeather.Weather + `
	，今天白天最高温度：` + todayWeather.Highest + "，夜晚最低温度：" + todayWeather.Lowest + "。<br/>钊哥哥温馨提示：" + todayWeather.Tips

	return info

}

type qWeatherValue struct {
	TextDay string `json:"textDay"`
	TempMax string `json:"tempMax"`
	TempMin string `json:"tempMin"`
	IconDay string `json:"iconDay"`
	Tips    string `json:"tips"`
}

type qWeatherResult struct {
	Code  string           `json:"code"`
	Daily []*qWeatherValue `json:"daily"`
}

type locationResult struct {
	Code     string     `json:"code,omitempty"`
	Location []location `json:"location"`
}
type location struct {
	Name string `json:"name,omitempty"`
	Id   string `json:"id,omitempty"`
	Lat  string `json:"lat,omitempty"`
	Lon  string `json:"lon,omitempty"`
}

func RequestGeoLocation(key string, location string) location {
	result := &locationResult{}
	res := ms.RequestGet("https://geoapi.qweather.com/v2/city/lookup?location=" + location + "&key=" + key)
	println("查询结果", res)
	err := json.Unmarshal(res, result)
	if err != nil {
		zap.L().Info("RequestGeoLocation序列化失败", zap.Error(err))
	}
	if result.Code != "200" {
		zap.L().Error("RequestGeoLocation请求失败")
	}

	return result.Location[0]
}

// 101200114 汉南
func RequestQweather(config interface{}, location string) string {
	conf := config.(*Config)
	result := &qWeatherResult{}
	locationObj := RequestGeoLocation(conf.Key, location)
	res := ms.RequestGet(conf.Url + "?key=" + conf.Key + "&location=" + locationObj.Id)

	err := json.Unmarshal(res, &result)
	if err != nil {
		zap.L().Info("RequestQweather序列化失败", zap.Error(err))
	}
	if result.Code == "402" {
		return "今天天气机器人卡壳啦，请充值让我修复它吧，hhh"
	}
	var now = result.Daily[0]
	weatherCode, _ := strconv.Atoi(now.IconDay)
	tips := weather.WeatherMapping[weatherCode].Tips
	info := "今天" + locationObj.Name + "天气" + now.TextDay + `
	，白天最高温度：` + now.TempMax + "摄氏度，夜晚最低温度：" + now.TempMin + "摄氏度。&nbsp;钊哥哥温馨提示：" + tips
	tempMax, _ := strconv.Atoi(now.TempMax)
	if tempMax <= 15 {
		info = info + "，温度较低，记得多穿点衣服哦"
	} else if tempMax > 35 {
		info = info + "温度有点高，记得多喝水，防止中暑"
	} else {
		info = info + "温度适中，玩得开心哈"
	}

	return info

}
